package com.hhkj.study.api.user.common.checker;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.study.admin.user.entity.User;
import com.hhkj.study.admin.user.service.extend.UserExtendService;
import com.hhkj.study.api.user.common.handler.AccountSessionHandler;
import com.hhkj.study.api.user.model.res.ApiCustomerUserBaseRes;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.regex.Pattern;

/**
 * AccountChecker
 *
 * @author SERVER
 * @since 2024/3/28 15:51
 */
@Component
public class AccountChecker {

    @Resource
    private UserExtendService userExtendService;

    /**
     * 校验密码
     * @param password 密码
     */
    public void checkPassword(String password) {
        if (password.length() < 6 || password.length() > 12) {
            throw new GlobalException("密码长度为 8 - 12 位");
        }
//        String reg = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,12}$";
//        if (!Pattern.matches(reg, password)) {
//            throw new GlobalException("密码长度为 8 - 12 位, 字母、数字两种组合");
//        }
    }

    /**
     * 校验注册手机号是否重复
     *
     * @param mobile 手机号码
     */
    public void checkRegisterMobile(String mobile) {
        User customerUser = userExtendService.getByMobileOrIdCard(mobile, null);
        if (ObjectUtil.isNotNull(customerUser)) {
            throw new GlobalException("该手机号已注册");
        }
    }


    /**
     * 校验账号是否有效
     * @return
     */
    public ApiCustomerUserBaseRes checkAccountValid(){
        ApiCustomerUserBaseRes currentAccount = AccountSessionHandler.getCurrentAccount();
        if (ObjectUtil.isNull(currentAccount) || SysConst.Flag.IS.getFlag().equals(currentAccount.getDisableFlag())) {
            throw new GlobalException("账号已删除或被已禁用，请联系管理员");
        }
        return currentAccount;
    }

}
